<?php
class Vertification_model extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }

	public function add($data) {   
		$this->db->insert("vertifications", $data);
		return $this->db->insert_id();
	}
	
	public function update($data, $id) {
		$this->db->where('id', $id);
		$this->db->update('vertifications', $data); 
		$this->db->where('id', $id);
		$vert = $this->db->get('vertifications')->row_array();
		if ($vert['type'] == 1) {
			if ($data['status'] == 1) {
				$sql = "update users set is_vertification=1 where id=" . $vert['created_by'];
			} else {
				$sql = "update users set is_vertification=0 where id=" . $vert['created_by'];
			}
			$this->db->query($sql);
		}
	}
	
	public function find_by_condition($condition, $offset = 0, $limit =20) {
		$sql = "select u.nickname,v.* from vertifications v 
				left join users u on u.id=v.created_by 
				where 1=1 " . $condition .
				" order by v.created desc limit " . $offset . "," . $limit;
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	
	public function get_count_by_condition($condition) {
		$sql = "select count(*) as total from vertifications v 
				where 1=1 " . $condition ;
		$result = $this->db->query($sql)->result_array();
		return $result['total'];
	}
}